package com.zsc.roadsys.controller;

import com.zsc.roadsys.constants.Constants;
import com.zsc.roadsys.entity.Role;
import com.zsc.roadsys.entity.User;
import com.zsc.roadsys.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SignController {
    @Autowired
    UserService userService;

    @GetMapping("userSign")
    public String userSign(){
        return "/login/sign";
    }

    @PostMapping("userSign")
    public String sign(@RequestParam("realName") String realName,
                     @RequestParam("password") String password,
                     @RequestParam("email") String email,
                     @RequestParam("phone") String phone){
        Md5Hash md5Hash3 = new Md5Hash(password,"salt",3);
        User user = User.builder().username(userService.CrUserIdByTime())
                                    .realName(realName)
                                    .password(md5Hash3.toHex())
                                    .email(email)
                                    .phone(phone)
                                    .enable(Constants.USER_ENABLE).build();
        userService.insertUser(user);
        return "/login/login";
    }
}
